package com.pcl.mvvm.utils

import com.witfortos.common.http.interceptor.Level
import com.witfortos.common.http.interceptor.LoggingInterceptor
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import okhttp3.internal.platform.Platform
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 *   @auther : tan
 *   time   : 2020-07-10
 */
class RetrofitClient {

    companion object {
        fun getInstance() = SingletonHolder.INSTANCE
        private lateinit var retrofit: Retrofit

        //        const val BASE_URL = "https://www.wanandroid.com/"
        const val BASE_URL = "http://hqzx-uat.witfort.cn"

    }

    private object SingletonHolder {
        val INSTANCE = RetrofitClient()
    }

    init {
        retrofit = Retrofit.Builder()
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()
    }

    private fun getOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
                .connectTimeout(20L, TimeUnit.SECONDS)
                .addNetworkInterceptor(LoggingInterceptor().apply {
                    isDebug = true
                    level = Level.BASIC
                    type = Platform.INFO
                    requestTag = "Request"
                    requestTag = "Response"
                })
                .writeTimeout(20L, TimeUnit.SECONDS)
                .connectionPool(ConnectionPool(8, 15, TimeUnit.SECONDS))
                .build()
    }

    fun <T> create(service: Class<T>?): T =
            retrofit.create(service!!) ?: throw RuntimeException("Api service is null!")
}